In this jupyter cookbook, we will explore the HAWC+ data cube and describe some of the basic analysis techniques involving imaging polarimetry data.
This cookbook follows the SOFIA press release of 30 Doradus observations: SOFIA Reveals Never-Before-Seen Magnetic Field Details.
The Level 4 reduced data from this program has been released immediately to the public and is available on the SOFIA Data Cycle System (DCS). This notebook will guide the reader through downloading the 30 Doradus data with a walkthrough of basic analysis techniques with python.
HAWC_PLUS from drop-down menuLEVEL_4 from drop-down menuSubmit buttonGet Selected Data in Current PageRequest Data BundleAfter downloading the SOFIA DCS bundle to your working directory you will want to unzip it, which will produce a directory structure like this:
Note that each file represents observations with a different filter. However, two observations were made with the same filter (HAWC C, $89\,\mathrm{\mu m}$). These files, F0483_HA_POL_7600014_HAWCHWPC_PMP_022-065.fits and F0484_HA_POL_7600017_HAWCHWPC_PMP_065-114.fits, were combined into one: level4->p5813->F0484_HA_POL_7600018_HAWCHWPC_PMP_022-114.fits.
You can choose to keep the fits files nested, or copy them into one directory.
For the purpose of this basic analysis, though, let us dump all the files into one sofia_data directory:
For this analysis, we require the standard numpy/scipy/matplotlib stack as well the astropy and aplpy modules.
With just a few lines of code, we can explore the HAWC+ fits data cubes and plot the images.
from astropy.io import fits
filename = 'sofia_data/F0485_HA_POL_76000110_HAWAHWPA_PMP_043-052.fits'
hawc = fits.open(filename)
hawc.info()
We can see above the data structure of the multi-extension fits files. Each file contains 19 extensions which encapsulates all of the Stokes parameters in a single package.
Stokes $I$---the zeroth extension in the fits file---represents the total intensity of the image, where $I^2 = Q^2 + U^2$.
Let us go ahead and plot this extension:
import matplotlib.pyplot as plt
%matplotlib notebook
# ^jupyter magic for inline plots
from aplpy import FITSFigure
# set colormap for all plots
cmap = 'viridis'
stokes_i = hawc['STOKES I'] # or hawc[0]. Note the extension is from the hawc.info() table above
fig = plt.figure(figsize=(7,7))
axs = FITSFigure(stokes_i, figure=fig) # load HDU into aplpy figure
axs.show_colorscale(cmap=cmap) # display the data with WCS projection and viridis colormap
# FORMATTING
axs.set_tick_labels_font(size='small')
axs.set_axis_labels_font(size='small')
# Add colorbar
axs.add_colorbar()
axs.colorbar.set_axis_label_text('Flux (Jy/pix)')
Similarly, we can plot the Stokes Q and Stokes U images:
stokes_q = hawc['STOKES Q']
stokes_u = hawc['STOKES U']
axq = FITSFigure(stokes_q, subplot=(1,2,1)) # generate FITSFigure as subplot to have two axes together
axq.show_colorscale(cmap=cmap) # show Q
axu = FITSFigure(stokes_u, subplot=(1,2,2),
figure=plt.gcf(), sharex=axq,sharey=axq) # shared so the axes zoom together
axu.show_colorscale(cmap=cmap) # show U
# FORMATTING
axq.set_title('Stokes Q')
axu.set_title('Stokes U')
axu.axis_labels.hide_y() # hide axis ticklabels for U figure
axu.tick_labels.hide_y()
axq.set_tick_labels_font(size='small')
axq.set_axis_labels_font(size='small')
axu.set_tick_labels_font(size='small')
axu.set_axis_labels_font(size='small')
We can additionally plot the associated error maps for each extension. [are the units for error in Jy/pixel or are they a percentage?]
stokes_q = hawc['STOKES Q']
error_q = hawc['ERROR Q']
axq = FITSFigure(stokes_q, subplot=(1,2,1)) # generate FITSFigure as subplot to have two axes together
axq.show_colorscale(cmap=cmap) # show Q
axe = FITSFigure(error_q, subplot=(1,2,2),
figure=plt.gcf(), sharex=axq,sharey=axq) # shared so the axes zoom together
axe.show_colorscale(cmap=cmap) # show error
# FORMATTING
axq.set_title('Stokes Q')
axe.set_title('Error Q')
axq.axis_labels.hide() # hide axis/tick labels
axe.axis_labels.hide()
axq.tick_labels.hide()
axe.tick_labels.hide()
Level 4 HAWC+ additionally provides extensions with the polarization percentage ($p$), angle ($\theta$), and their associated errors ($\sigma$).
Percent polarization ($p$) and error ($\sigma_p$) are calculated as:
$p = 100\sqrt{\left(\frac{Q}{I}\right)^2+\left(\frac{U}{I}\right)^2}$
$\sigma_p = \frac{100}{I}\sqrt{\frac{1}{(Q^2+U^2)}\left[(Q\,\sigma_Q)^2+(U\,\sigma_U)^2+2QU\,\sigma_{QU}\right]+\left[\left(\frac{Q}{I}\right)^2+\left(\frac{U}{I}\right)^2\right]\sigma_I^2-2\frac{Q}{I}\sigma_{QI}-2\frac{U}{I}\sigma_{UI}}$ .
Note that $p$ here represents the percent polarization as opposed to the more typical convention for $p$ as the fractional polarization.
Maps of these data are found in extensions 7 (PERCENT POL) and 9 (ERROR PERCENT POL).
Polarized intensity, $I_p$, can then be calculated as $I_p = \frac{I\times p}{100}$, which is included in extension 13 (POL FLUX).
Also included is the debiased polarization percentage ($p^\prime$) calculated as:
$p^\prime=\sqrt{p^2-\sigma_p^2}$, found in extension 8 (DEBIASED PERCENT POL).
We similarly define the debiased polarized intensity as $I_{p^\prime} = \frac{I\times p^\prime}{100}$, which is included in extension 15 (DEBIASED POL FLUX).
stokes_ip = hawc['DEBIASED POL FLUX']
axi = FITSFigure(stokes_i, subplot=(1,2,1))
axi.show_colorscale(cmap=cmap) # show I
axp = FITSFigure(stokes_ip, subplot=(1,2,2),
figure=plt.gcf(), sharex=axq,sharey=axq)
axp.show_colorscale(cmap=cmap) # show Ip
# FORMATTING
axi.set_title(r'$I$')
axp.set_title(r'$I_{p^\prime}$')
axi.axis_labels.hide() # hide axis/tick labels
axp.axis_labels.hide()
axi.tick_labels.hide()
axp.tick_labels.hide()
From the $Q$ and $U$ maps, the polarization angle $\theta$ is calculated in the standard way:
$\theta = \frac{90}{\pi}\,\mathrm{tan^{-1}}\left(\frac{U}{Q}\right)$
with associated error:
$\sigma_\theta = \frac{90}{\pi\left(Q^2+U^2\right)}\sqrt{\left(Q\sigma_Q\right)^2+\left(U\sigma_U\right)^2-2QU\sigma_{QU}}$
The angle map is stored in extension 10 (POL ANGLE), with its error in extension 12 (ERROR POL ANGLE).
However, these angles are relative to detector coordinates. The angle we are more interested in is the angle on the sky. As part of the HAWC+ reduction pipeline, $\theta$ is corrected for the vertical position angle of the instrument on the sky, the angle of the HWP plate, as well as an offset angle that is calibrated to each filter configuration. This correction angle is applied to $\theta\rightarrow\theta^\prime$ and is saved to extension 11 (ROTATED POL ANGLE). This also affects the Stokes $Q$ and $U$ parameters, and so this factor has already been rolled into the STOKES Q and STOKES U extensions (and their corresponding error maps) in the HAWC+ data cube.
We can now use the $p^\prime$ and $\theta^\prime$ maps to plot the polarization vectors.
We perform the following steps:
import numpy as np
from astropy.stats import sigma_clip
filename = 'sofia_data/F0485_HA_POL_76000110_HAWAHWPA_PMP_043-052.fits'
hawc = fits.open(filename)
p = hawc['DEBIASED PERCENT POL'] # %
error_p = hawc['ERROR PERCENT POL'] # %
theta = hawc['ROTATED POL ANGLE'] # deg
stokes_ip = hawc['DEBIASED POL FLUX'] # Ip'
# 1. plot Stokes I
axi = FITSFigure(stokes_i)
# 2. perform sigma clip on Ip'
clipped_ip = sigma_clip(stokes_ip.data, sigma_lower=2, sigma_upper=3,
cenfunc=np.nanmedian, stdfunc=np.nanstd)
# 3. mask out low S/N vectors by setting masked indices to NaN
# 3a. first, remove
p.data[clipped_ip.mask] = np.nan
#idx = np.where(p.data>5)
#p.data[idx] = np.nan
# 3a. mask out
p.data *= 0.05
axi.show_vectors(p, theta)
axi.show_colorscale(cmap=cmap)
import numpy as np
p = hawc['DEBIASED PERCENT POL'] # %
theta = hawc['ROTATED POL ANGLE'] # deg
# set hi/low sigma cut, and compute stats
low, hi = (1, 3)
med = np.nanmedian(stokes_i.data)
sd = np.nanstd(stokes_i.data)
lowscale = med - (low * sd)
hiscale = med + (hi * sd)
# clip p and theta on sigma cuts
idx = np.where((stokes_i.data >= hiscale) & (stokes_i.data <= lowscale))
p.data[idx] = np.nan
theta.data[idx] = np.nan
axi = FITSFigure(stokes_i)
axi.show_vectors(p, theta)
axi.show_colorscale(cmap=cmap,vmin=lowscale,vmax=hiscale)